import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @author lys
 * 本页面是添加图书页面
 */

public class AddBook extends JFrame {
    private static AddBook instance;

    private JLabel jLabel = new JLabel("增加图书");
    private JLabel jLabel1 = new JLabel(" 类别 :");
    private JLabel jLabel2 = new JLabel(" 书名 :");
    private JLabel jLabel3 = new JLabel(" 作者 :");
    private JLabel jLabel4 = new JLabel("出版社:");

    private JComboBox<String> box = new JComboBox<String>();

    private JTextArea jTextArea1 = new JTextArea();
    private JTextArea jTextArea2 = new JTextArea();
    private JTextArea jTextArea3 = new JTextArea();

    private JButton button1 = new JButton("确认添加");
    private JButton button2 = new JButton("返回");

    private Font font1 = new Font("宋体",Font.BOLD,60);
    private Font font2 = new Font("宋体",Font.BOLD,40);
    private Font font3 = new Font("宋体",Font.BOLD,30);

    private JPanel jPanel1 = new JPanel();
    private JPanel jPanel2 = new JPanel();
    private JPanel jPanel3 = new JPanel();
    private JPanel jPanel4 = new JPanel();

    private AddBook() {
        ArrayList<String> typeList = getTypeList();
        for(String str: typeList){
            box.addItem(str);
        }

        Icon i = new ImageIcon(AddBook.class.getResource("img/login2.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 800, 800);

        jLabel.setFont(font1);
        jLabel.setBounds(280, 50, 300, 80);
        jLabel.setForeground(Color.white);

        //类别
        jLabel1.setFont(font2);
        jLabel1.setBounds(150,270,200,50);
        jLabel1.setForeground(Color.white);
        box.setPreferredSize(new Dimension(350,50));
//        box.addItem("数学");
//        box.addItem("语文");
        box.setFont(font3);
        //box.setBounds(400,200,200,40);
        box.setOpaque(false);
        jPanel1.setBounds(0,230,800,60);
        jPanel1.add(jLabel1);
        jPanel1.add(box);

        //书名
        jLabel2.setFont(font2);
        jLabel2.setBounds(150, 360, 200, 50);
        jLabel2.setForeground(Color.white);
        //jTextArea1.setLayout(null);
        jTextArea1.setPreferredSize(new Dimension(350,50));
        jTextArea1.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jTextArea1.setFont(font2);
        jTextArea1.setForeground(Color.black);
        jPanel2.setBounds(0,320,800,60);
        jPanel2.add(jLabel2);
        jPanel2.add(jTextArea1);

        //作者
        jLabel3.setFont(font2);
        jLabel3.setBounds(150, 420, 200, 50);
        jLabel3.setForeground(Color.white);
        //jTextArea1.setLayout(null);
        jTextArea2.setPreferredSize(new Dimension(350,50));
        jTextArea2.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jTextArea2.setFont(font2);
        jTextArea2.setForeground(Color.black);
        jPanel3.setBounds(0,410,800,60);
        jPanel3.add(jLabel3);
        jPanel3.add(jTextArea2);

        //出版社
        jLabel4.setFont(font2);
        jLabel4.setBounds(150, 500, 200, 50);
        jLabel4.setForeground(Color.white);
        //jTextArea1.setLayout(null);
        jTextArea3.setPreferredSize(new Dimension(350,50));
        jTextArea3.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jTextArea3.setFont(font2);
        jTextArea3.setForeground(Color.black);
        jPanel4.setBounds(0,500,800,60);
        jPanel4.add(jLabel4);
        jPanel4.add(jTextArea3);

        button1.setBounds(280,600,300,60);
        button1.setFont(font2);
//        button1.setForeground(Color.white);
        button2.setBounds(620,650,100,50);
//        button2.setForeground(Color.white);
        button2.setFont(font3);

        jPanel1.setOpaque(false);
        jPanel2.setOpaque(false);
        jPanel3.setOpaque(false);
        jPanel4.setOpaque(false);

        jTextArea1.setOpaque(false);
        jTextArea2.setOpaque(false);
        jTextArea3.setOpaque(false);

        add(jLabel);
        add(jPanel1);
        add(jPanel2);
        add(jPanel3);
        add(jPanel4);
        add(button1);
        add(button2);
        add(Label);

        setSize(800,800);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String bookName = jTextArea1.getText();
                String writer = jTextArea2.getText();
                String press = jTextArea3.getText();
                String type = box.getSelectedItem().toString();
                addBook(bookName, press, type, writer);
                ManageBook.getInstance().showBooks();
                removeInstance();
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                AdminInterface.getInstance().admin.setVisible(true);
            }
        });

        button1.setFocusPainted(false);
        button2.setFocusPainted(false);

    }
    public static AddBook getInstance() {
        if(instance == null) instance=new AddBook();
        return instance;
    }

//    public static void main(String[] args) {
//        AddBook.getInstance();
//    }
    /**
     * @author zj
     */
    /**
     *
     * @return 所有书籍类别
     */
    public static ArrayList<String> getTypeList(){
        ResultSet rs;
        String sql = "select * from typelist";
        ArrayList<String> typeList = new ArrayList<>();
        try{
            PreparedStatement preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
            rs = preSql.executeQuery();
            while(rs.next()){
                String type = rs.getString("type");
                typeList.add(type);
            }
        }catch(Exception e){
            System.out.println(e);
        }
        return typeList;
    }

    /**
     * 添加书籍到数据库中
     * @param bookName
     * @param press
     * @param type
     * @param writer
     */
    public void addBook(String bookName, String press, String type, String writer){
        if(bookName.length() == 0 || press.length() == 0 || writer.length() == 0){
            JOptionPane.showMessageDialog(null, "输入信息不能为空！");
            return;
        }
        String sql = "insert into book(bookname, press, type, writer, isborrowed) values(?, ?, ?, ?, 0)";
        try{
            PreparedStatement preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
            preSql.setString(1,bookName);
            preSql.setString(2, press);
            preSql.setString(3, type);
            preSql.setString(4, writer);
            int ok = preSql.executeUpdate();
            System.out.println(ok+ " line is affected");
        }catch (Exception e){
            System.out.println(e);
        }
        JOptionPane.showMessageDialog(null, "增加新书成功");
        setVisible(false);
        AdminInterface.getInstance().admin.setVisible(true);
    }
    public static void removeInstance(){
        instance = null;
    }
}
